<?php

namespace app\stock\controller;

use think\Controller;

class Dashboard extends Controller
{
    // 用户模型
    protected $BusinessModel;
    // 订单模型
    protected $OrderModel;
    // 客户回访模型
    protected $VisitModel;
    // 客户领取模型
    protected $ReceiveModel;
    // 客户来源模型
    protected $SourceModel;
    // 时间列表
    protected $TimeList;
    protected $SubjectOrderModel;
    private $AdminModel;
    protected $relationSearch = true;
    public function _initialize()
    {
        // 继承父类
        parent::_initialize();
        $this->BusinessModel = model('Business.Business');
        $this->OrderModel = model('Product.Order.Order');
        $this->VisitModel = model('Business.Visit');
        $this->ReceiveModel = model('Business.Receive');
        $this->SourceModel = model('Business.Source');
        $this->AdminModel = model('Admin');
        $this->SubjectOrderModel = model('Subject.Order');
        $year = date("Y", time()); //获取年份

        $this->TimeList = [];

        // 时间封装
        for ($i = 1; $i <= 12; $i++) {
            $time = strtotime($year . '-' . $i);

            //获取每个月的第一天
            $start = date('Y-m-01', $time);

            $end = date('Y-m-t', $time);

            $this->TimeList[] = [strtotime($start), strtotime($end)];
        }
    }
     // 统计
     public function total()
     {
         // 订单数量
         $ordercount = $this->OrderModel->count();
 
         // 销售额
         $amount = $this->OrderModel->sum('amount');
 
         // 客户数量
         $busidcount = $this->BusinessModel->count();
 
         $data = [
             'ordercount' => $ordercount,
             'amount' => $amount,
             'busidcount' => $busidcount
         ];
 
         $this->success('', '', $data);
     }
 
     // 用户统计
     public function business()
     {
         foreach ($this->TimeList as $item) {
 
             // 已认证
             $certified[] = $this->BusinessModel->where(['auth' => '1', 'createtime' => ['between', $item]])->count();
             // 未认证
             $nocertified[] = $this->BusinessModel->where(['auth' => '0', 'createtime' => ['between', $item]])->count();
         }
 
         $data = [
             'certified' => $certified,
             'nocertified' => $nocertified
         ];
 
         $this->success('', '', $data);
     }
 
     // 回访
     public function visit()
     {
         foreach ($this->TimeList as $item) {
             $data[] =  $this->VisitModel->where('createtime','between', $item)->count();
         }
 
         $this->success('', '', $data);
     }
 
     //领取
     public function receive()
     {
         foreach ($this->TimeList as $item) {
             // 申请
             $apply[] = $this->ReceiveModel->where(['status' => 'apply', 'applytime' => ['between', $item]])->count();
             // 分配
             $allot[] = $this->ReceiveModel->where(['status' => 'allot', 'applytime' => ['between', $item]])->count();
             // 回收
             $recovery [] =  $this->ReceiveModel->where(['status' => 'recovery', 'applytime' => ['between', $item]])->count();
         }
 
         $data = [
             'apply' => $apply,
             'allot' => $allot,
             'recovery' => $recovery
         ];
 
         $this->success('', '', $data);
     }
 
     // 订单
     public function order()
     {
         $data = [];
 
         $nopay = $this->OrderModel->where('status','0')->count();
 
         $data[] = [
             'name' => '待支付',
             'value' => $nopay,
         ];
 
         $pay = $this->OrderModel->where('status','1')->count();
 
         $data[] = [
             'name' => '待发货',
             'value' => $pay,
         ];
 
         $send = $this->OrderModel->where('status','2')->count();
 
         $data[] = [
             'name' => '待收货',
             'value' => $send,
         ];
 
         $rate = $this->OrderModel->where('status','3')->count();
 
         $data[] = [
             'name' => '待评价',
             'value' => $rate,
         ];
 
         $accomplish = $this->OrderModel->where('status','4')->count();
 
         $data[] = [
             'name' => '已完结',
             'value' => $accomplish,
         ];
 
         $this->success('', '', $data);
     }
 
     // 来源
     public function source() 
     {
         $sourcelist = $this->SourceModel->select();
 
         $data = [];
 
         foreach ($sourcelist as $item)
         {
             $count = $this->BusinessModel->where('sourceid', $item['id'])->count();
 
             $data[] = [
                 'name' => $item['name'],
                 'value' => $count
             ];
         }
         
         $this->success('', '', $data);
     }
    //  领取记录
    public function receivelist(){
        $adminid = $this->request->param('adminid', 0, 'trim');
       
        $admininfo = $this->AdminModel->find($adminid);
       
        if (!$admininfo) {
            $this->error('管理不存在');
        }
        $list = $this->ReceiveModel->with('business')->where('applyid',$adminid)->select();
        $this->success('','',$list);
    }
    // 课程订单
    public function orderlist(){
        $data = $this->SubjectOrderModel->with(['subject', 'business'])->order('createtime', 'DESC')->select();
        $this->success('','', $data);
    }
    // 课程订单删除
    public function orderdel()
    {
        $adminid = $this->request->param('adminid', 0, 'trim');
        $orderid = $this->request->param('orderid', 0, 'trim');

        $admininfo = $this->AdminModel->find($adminid);

        if (!$admininfo) {
            $this->error('管理不存在');
        }

        $orderinfo = $this->SubjectOrderModel->find($orderid);

        if (!$orderinfo) {
            $this->error('课程订单不存在');
        }

        $result = $this->SubjectOrderModel->destroy($orderid);

        if($result === false)
        {
            $this->error('删除失败'); 
        } else {
            $this->success('删除成功');
        }
    }
}